/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if(root == nullptr) return nullptr;

        if(root->val == key) {
            TreeNode* leftTree = root->left;
            TreeNode* rightTree = root->right;

            if(leftTree == nullptr) return rightTree;
            if(rightTree == nullptr) return leftTree;
            TreeNode* temp = rightTree;
            while(temp->left != nullptr){
                temp = temp->left;
            }
            temp->left = leftTree;

            return rightTree;
        }

        if(root->val < key) root->right = deleteNode(root->right, key);
        if(root->val > key) root->left = deleteNode(root->left, key);

        return root;
    }
};